'''
Created on Apr 28, 2011

@author: GSE
'''

import urllib
import xml.dom.minidom
import fileinput


url = "http://www.thetvdb.com/api/6F53113AF4976B22/mirrors.xml"
content = urllib.urlopen(url).read()

dom = xml.dom.minidom.parseString(content)

mirrorpath =  dom.getElementsByTagName("mirrorpath")[0].firstChild.data

input = "Breaking Bad".replace(" ", "+")
url2 = "http://www.thetvdb.com/api/GetSeries.php?seriesname="+input
content2 = urllib.urlopen(url2).read()
dom = xml.dom.minidom.parseString(content2)

print content2


if(content2 != '<?xml version="1.0" encoding="UTF-8" ?>\n<Data>\n</Data>'):
    seriesid = dom.getElementsByTagName("seriesid")[0].firstChild.data
    print "Found"
else:
    seriesid = "0"
    print" Not found"
#print content2
#print "\n" + mirrorpath
#print seriesid

print "-----------------------------"

if(seriesid != "0"):
    url3 = mirrorpath+"/api/6F53113AF4976B22/series/"+seriesid+"/all"
    content3 = urllib.urlopen(url3).read()

    dom = xml.dom.minidom.parseString(content3)
    infonode =dom.getElementsByTagName("Series")[0]

    print infonode.toxml() #from here, take the info, build Content and put it into DB